<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script>
        'use strict';

        var debug = true;
        if (debug == false) {
            var alert = function () {
            };
        }

        var agent = {
            navigator: navigator.userAgent,
            match: function (Reg) {
                return this.navigator.match(Reg);
            },
            isWechat: navigator.userAgent.match(/MicroMessenger/g) != null,
            isClient: navigator.userAgent.match(/Tattoo/g) != null,
            isAndroid: navigator.userAgent.match(/Android/gi) != null,
            isIos: navigator.userAgent.match(/ios/gi) != null
        };
        //尝试从cookie获取参数
        var cookie = {
            openId: document.cookie.match(/openId=([^ ;]+)/),
            code: document.cookie.match(/code=([^ ;]+)/),
            state: document.cookie.match(/state=([^ ;]+)/),
            clear: function (key) {
                document.cookie = key + '=';
            }
        };
        var query = {
            code: location.href.match(/code=([^&#]+)/),
            state: location.href.match(/state=([^&#]+)/)
        };
        var getHistoryLength = function () {
            if (agent.match(/wxdebugger/g) != null) {
                return history.length - 1;
            } else {
                return history.length;
            }
        };
        //用于存储数据
        var db = {
            appid: 'wxe5e08d0563591e11'
        };

        if (!debug) {
            //预处理
            //todo 记得把这段注回来
            if (agent.isClient) {
                //客户端策略
                //todo 获取手机号用以获取session

            } else if (agent.isWechat) {
                //微信策略
                if (cookie.openId != null) {
                    //获取到openId
                    db.wx_openId = cookie.openId[1];
                } else if (cookie.code != null) {
                    //获取到code
                    db.wx_code = cookie.code[1];
                    cookie.clear('code');

                } else if (query.code != null) {
                    //尝试从首页的URL中获取code
                    document.cookie = 'code=' + query.code[1];
                    if (query.state != null) {
                        document.cookie = 'state=' + query.state[1];
                    }
                    if (agent.match(/MQQBrowser/gi) != null) {
                        history.back();
                    } else {
                        history.back();
                    }

                } else {
                    //IOS我觉得还能抢救一下，申请下授权好了
                    var redirectUrl = encodeURIComponent(location.origin + location.pathname + '#/home/' + (query.state != null ? ('&state=' + query.state[1]) : ''));
                    var authUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + db.appid
                            + '&redirect_uri=' + redirectUrl
                                //                            + '&response_type=code&scope=snsapi_base'
                            + '&response_type=code&scope=snsapi_userinfo'
                            + '#wechat_redirect';
                    location.href = authUrl;
                }
            }
        }

        //
        //页面定向
        function redirect() {
            var redirect = query.state || cookie.state;
            cookie.clear('state');
            if (cookie.state != null || (query.state != null && getHistoryLength() < 2)) {
                setTimeout(function () {
                    var stack = redirect[1].split('-');
                    for (var i = 0; i < stack.length; i++) {
                        location.hash = stack[i];
                    }
                }, 100);
            }
        }

        //构造入口文件
        var m = document.createElement("script");
        m.setAttribute("src", "http://cdn.bootcss.com/require.js/2.1.22/require.min.js");
        m.setAttribute("data-main", "loaderMap.js?" + new Date().getTime());
                document.head.appendChild(m);
    </script>
    <meta name="format-detection" content="telephone=no"/>
    <meta name="viewport"
          content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <!--<script src="http://cdn.bootcss.com/require.js/2.1.22/require.min.js" data-main="loaderMap.js"></script>-->
</head>
<body onload="redirect()">

<div id="dialog">
    <div id="dialogTips"></div>
</div>

<div id="animation"></div>

<div id="logger"></div>
<div id="payment"></div>

<div id="page"></div>

</body>
</html>